package com.carsystem.service.impl;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.carsystem.config.Alipay;
import com.carsystem.entity.*;
import com.carsystem.mapper.*;
import com.carsystem.request.AliPayBean;
import com.carsystem.request.FrontOrderRequestVo;
import com.carsystem.request.OrderRequestVo;
import com.carsystem.request.SetOrderRequestVO;
import com.carsystem.response.FrontOrderResponseVo;
import com.carsystem.response.OrderResponseVo;
import com.carsystem.service.IOrderService;
import com.carsystem.utils.PageHandler;
import com.carsystem.utils.PageResult;
import com.carsystem.utils.QueryPageBean;
import com.carsystem.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ykl
 * @since 2021-12-04
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private Alipay alipay;
    @Resource
    private OpenrendMapper openrendMapper;
    @Resource
    private TReturncarMapper returncarMapper;

    @Override
    public Result updateStatus(Integer id) {
        Order order = orderMapper.selectById(id);
        order.setOrderStatus(1);
        int i = orderMapper.updateById(order);
        TReturncar tReturncar = new TReturncar();
        tReturncar.setOrderId(order.getOrderId());
        tReturncar.setCompensationAmount(new BigDecimal(0));
        tReturncar.setReturnState(0);
        returncarMapper.insert(tReturncar);
        if (i>0){
            return new Result(true,"审核通过",order);
        }
        return new Result(false,"审核未通过请查询原因");
    }

    @Override
    public PageHandler selectOrderByPage(OrderRequestVo orderRequestVo) {
        Map<String, Object> map = new HashMap<>();
        Integer offset = (orderRequestVo.getPageIndex() - 1) * orderRequestVo.getPageSize();
        map.put("offset",offset);
        map.put("pageSize", orderRequestVo.getPageSize());
        Long orderId1 = orderRequestVo.getOrderId();
        String orderId = String.valueOf(orderId1);
        if (StringUtils.isNotBlank(orderId)){
            map.put("orderId",orderRequestVo.getOrderId());
        }
        if (StringUtils.isNotBlank(orderRequestVo.getMemberName())){
            map.put("memberName",orderRequestVo.getMemberName());
        }
        if (StringUtils.isNotBlank(orderRequestVo.getCarName())){
            map.put("carName",orderRequestVo.getCarName());
        }
        if (StringUtils.isNotBlank(orderRequestVo.getPhoneNumber())){
            map.put("phoneNumber",orderRequestVo.getPhoneNumber());
        }
        Integer count = orderMapper.getOrderListCount(map);
        List<OrderResponseVo> orderList = orderMapper.getOrderList(map);
        for (OrderResponseVo orderResponseVo : orderList) {
            System.out.println(orderResponseVo);
        }
        PageHandler pageHandler = new PageHandler(orderRequestVo.getPageIndex(), orderRequestVo.getPageSize(),count);
        pageHandler.setList(orderList);
        return pageHandler;
    }

    @Override
    public Result updateOrderResponse(OrderResponseVo orderResponseVo) {
        boolean b = orderMapper.updateOrderResponse(orderResponseVo);
        if (orderResponseVo.getPayStatus()==1){
            TReturncar tReturncar = new TReturncar();
            tReturncar.setOrderId(orderResponseVo.getOrderId());
            tReturncar.setCompensationAmount(new BigDecimal(0));
            tReturncar.setReturnState(0);
            returncarMapper.insert(tReturncar);
        }

        if (b){
            return new Result(true,"修改成功",b);
        }
        return new Result(false,"修改失败");
    }

    @Override
    public boolean setOrder(SetOrderRequestVO setOrderRequestVO) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("member_id",setOrderRequestVO.getMemberId());
        Member member1 = memberMapper.selectOne(memberQueryWrapper);
        member1.setMemberName(setOrderRequestVO.getMemberName());
        member1.setIdcard(setOrderRequestVO.getIdCard());
        memberMapper.update(member1,memberQueryWrapper);
        QueryWrapper<Openrend> openrendQueryWrapper = new QueryWrapper<>();
        openrendQueryWrapper.eq("shop_id",setOrderRequestVO.getShopId()).eq("car_id",setOrderRequestVO.getCarId());
        Openrend openrend = openrendMapper.selectOne(openrendQueryWrapper);
        openrend.setRentNum(openrend.getRentNum()-1);
        if (openrend.getRentNum()==0){
            return false;
        }
        openrendMapper.update(openrend,openrendQueryWrapper);
        setOrderRequestVO.setPayStatus(0);
        return orderMapper.setOrder(setOrderRequestVO);

    }

    @Override
    public String aliPay(AliPayBean aliPayBean){
        String pay=null;
        try {
            pay = alipay.pay(aliPayBean);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return pay;
    }

    @Override
    public List<FrontOrderResponseVo> selectOrderById(String memberId) {
        return  orderMapper.selectOrderById(memberId);
    }

    @Override
    public PageHandler selectOrderByIdList(FrontOrderRequestVo frontOrderRequestVo) {
        Map<String, Object> map = new HashMap<>();
        Integer offset = (frontOrderRequestVo.getPageIndex() - 1) * frontOrderRequestVo.getPageSize();
        map.put("offset",offset);
        map.put("pageSize", frontOrderRequestVo.getPageSize());

        if (StringUtils.isNotBlank(frontOrderRequestVo.getMemberId())){
            map.put("memberId",frontOrderRequestVo.getMemberId());
        }

        Integer count = orderMapper.getOrderByIdListCount(map);
        List<FrontOrderResponseVo> orderList = orderMapper.getOrderByIdList(map);
        PageHandler pageHandler = new PageHandler(frontOrderRequestVo.getPageIndex(), frontOrderRequestVo.getPageSize(),count);
        pageHandler.setList(orderList);
        return pageHandler;
    }
}
